package flatlandAgent;

import evolutionaryProblems.Phenotype;
import fitnessFunctions.FitnessFunction;
import genotype.FlatlandGenotype;
import genotype.Genotype;

public class FlatlandPhenotype extends Phenotype {
	
	FlatlandGenotype genotype; 
	FitnessFunction fitnessFunction; 
	
	public FlatlandPhenotype(FlatlandGenotype genotype) {
		this.genotype = genotype;
		this.fitnessFunction = genotype.getParameters().getFitnessFunction();
		calculateFitness();
	}

	@Override
	public void calculateFitness() {
		this.fitness = fitnessFunction.calculateFitness(this);
	}

	@Override
	public Genotype getGenotype() {
		return genotype;
	}

	@Override
	public boolean isSolution() {
		return fitness >= 1.0;
	}
	
	@Override
	public double getFitness(){
		if(isEditable()){
			FitnessFunction newFitnessFunction = genotype.getParameters().getFitnessFunction(); 
			if (fitnessFunction != newFitnessFunction){
				fitnessFunction = newFitnessFunction; 
//			System.out.println("Old fitness function: " + fitness);
				calculateFitness(); 
//			System.out.println("New fitness function: " + fitness);
			}
			return this.fitness; 
		} else {
			return this.fitness; 
		}
	}

	@Override
	public boolean equals(Object o){
		if (o instanceof FlatlandPhenotype){
			if (((FlatlandParameters)genotype.getParameters()).isDynamicMap()) {
//				System.out.println(this);
//				System.out.println(o);
				return this.phenotypeID == ((FlatlandPhenotype)o).phenotypeID;
			}
			return this.getFitness() == ((FlatlandPhenotype)o).getFitness();
//					&& this.fitnessFunction == ((FlatlandPhenotype)o).fitnessFunction; 
		}
		return false; 
	}
	
	public FlatlandFitnessFunction getFitnessFunction(){
		return (FlatlandFitnessFunction) fitnessFunction; 
	}
	
}
